<template>
  <el-container>
    <!--  左边树节点  -->
    <el-container style="width: 17%;">
      <el-header style="width: 100%;">
        <el-input placeholder="请输入内容" size="medium" prefix-icon="el-icon-search" v-model="orgName"></el-input>
      </el-header>
      <el-main>
        <el-tree :data="data" :props="defaultProps"></el-tree>
      </el-main>
    </el-container>
    <!--  右边数据表格  -->
    <el-container style="width: 82%;">
      <el-header>
        <el-row :gutter="15">
          <el-col :span="6">
            <el-input placeholder="请输入流水号" size="medium" prefix-icon="el-icon-search" v-model="orderId"></el-input>
          </el-col>
          <el-col :span="3">
            <el-select v-model="billState" @change="queryData" size="medium" placeholder="请选择">
              <el-option v-for="item in billStates" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-col>
          <el-col :span="4">
            <el-button type="success" size="small" icon="el-icon-search" @click="queryData">查询</el-button>
            <el-button type="warning" size="small" icon="el-icon-refresh" @click="clearCondition">重置</el-button>
          </el-col>
        </el-row>
      </el-header>
      <el-main>
        <el-table :data="orderData" style="width: 100%" height="100%" border :cell-style="{'text-align': 'center'}" :header-cell-style="{'text-align': 'center'}">
          <el-table-column prop="f_out_trade_no" label="流水号"  width="300"></el-table-column>
          <el-table-column prop="f_bill_state" label="订单状态"></el-table-column>
          <el-table-column prop="f_total_fee" label="交易金额"></el-table-column>
          <el-table-column prop="f_create_time" label="交易时间" width="300"></el-table-column>
          <el-table-column prop="f_query_times" label="查询次数"></el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button @click.native.prevent="refund(scope.$index)" type="danger" size="medium" :disabled="orderData[scope.$index].id !== -1">
                退款
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>
    <el-dialog title="退款提示" :visible.sync="dialogVisible" width="30%" append-to-body>
      <label>
        <textarea id = "cli" v-model="refundOrder" style="opacity:0;position:absolute;"></textarea>
      </label>
      <span>确认对订单【
        <el-tooltip class="item" effect="light" content="点击复制" placement="top">
          <el-button type="text" @click="copyCli">{{refundOrder}}</el-button>
        </el-tooltip>
      】进行退款？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="refundConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 剪切板 -->
  </el-container>
</template>

<script>
export default {
  name: 'Refund',
  methods: {
    // 查询用户数据
    queryData () {
      // 组织查询条件
      let condition = '1 = 1'
      if (this.orderId !== '') {
        condition += ' and f_out_trade_no like \'%' + this.orderId + '%\''
      }
      if (this.billState !== '') {
        condition += ' and f_bill_state = ' + this.billState
      }
      let that = this
      // 查询
      this.axios.post('/sql/getRefundOrder', {'data': {'condition': condition}})
        .then(function (response) {
          that.orderData = response.data
        }).catch(function (error) {
          that.$message.error('数据加载失败!错误码:PAY10086' + error)
        })
    },
    // 重置搜索条件
    clearCondition () {
      this.orderId = ''
      this.billState = ''
    },
    refund (index) {
      this.dialogVisible = true
      this.refundOrder = this.orderData[index].f_out_trade_no
    },
    // 发起退款
    refundConfirm () {
      this.dialogVisible = false
      // 退款
      let that = this
      this.axios.post('/logic/refund', {'data': {'f_out_trade_no': this.refundOrder}})
        .then(function (response) {
          that.$message({
            message: '订单【' + that.refundOrder + '】发起退款成功，请稍后查询订单状态！',
            type: 'success'
          })
        }).catch(function (error) {
          that.$message.error('数据加载失败!错误码:PAY10086' + error)
        })
    },
    // 流水号复制到剪切板
    copyCli () {
      let order = document.getElementById('cli')
      order.select()
      document.execCommand('Copy')
      this.$message({
        message: '复制成功！',
        type: 'success'
      })
    }
  },
  data () {
    return {
      orgName: '',
      // 表格数据
      orderData: [],
      // 查询条件
      orderId: '',
      billState: '',
      // 显示删除提示框
      dialogVisible: false,
      // 退款流水号
      refundOrder: '',
      billStates: [
        {
          value: 0,
          label: '等待支付'
        },
        {
          value: 1,
          label: '支付成功'
        },
        {
          value: 2,
          label: '支付失败'
        },
        {
          value: 3,
          label: '已经退款'
        }
      ],
      data: [
        {
          label: '一级 1'
        },
        {
          label: '一级 2'
        },
        {
          label: '一级 3'
        }],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      formLabelWidth: '120px'
    }
  },
  mounted () {
  }
}
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.el-table__header tr,
.el-table__header th {
  padding: 0;
  height: 40px;
  line-height: 30px;
  text-align: center;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
